<template>
  <div class="login-big">
    <div class="login-sollr">
       <img src="../assets/logo.png" alt="">
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="账号" prop="username">
    <el-input v-model="ruleForm.username"></el-input>
  </el-form-item>
   <el-form-item label="密码" prop="password">
    <el-input v-model="ruleForm.password"></el-input>
  </el-form-item>
</el-form>
<el-row>
  <el-button type="danger"  @click="login">登录</el-button>
  <el-button type="danger" >重置</el-button>
</el-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {},
  methods: {
    //调用的登录的方法
    login() {
      //绑定ref上的名字 表单校验      从这开始是异步请求数据  第一个参数是成功 第二个是失败
      this.$refs.ruleForm.validate(async (success, errorFields) => {
        //判断成功的话就请求数据
        if (success) {
          try {
            //请求数据成功后解构赋值
            const { data: { data, meta: { status } } } = await this.$apis.login(
              this.ruleForm
            );
            console.log(data);
            //成功以后的操作
            if (status == 200) {
              const { token } = data;
              sessionStorage.setItem("token", token);
              this.$message.success("登录成功");
              this.$router.push("/home");
            }
            //如果请求失败打印错误信息
          } catch (error) {
            console.log(error);
          }
          //否则就执行失败的代码
        } else {
          // 接受错误的变量
          var message = "";
          //循环的json文件的信息 那个数据错误返回那天msg
          for (var key in errorFields) {
            var errorObj = errorFields[key];
            errorObj.forEach(item => {
              message += item.message + "\n";
            });
          }
          // 信息错误
          alert(message);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
  .login-big{
    position: absolute;
    top: 336px;
    left: 426px;
    background: wheat;
    width: 500px;
    height: 300px;
  }
  .login-sollr{
    position: absolute;
    top: -103px;
    left: 143px;
    background: white;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    box-shadow: 0 0 20px #fff;
    img{
      width: 80%;
      height: 89%;
      position: relative;
      top: 30px;
      left: 18px;
    }
  }
  .demo-ruleForm{
    margin-top: 120px;
    .el-input{
    width: 90%
    }
  }
  .el-button{
    margin-left: 130px;
  }
</style>
